package com.example.demo.internettongxin.tcptongxin;

/**
 * @Description Tsercer
 * @Author songchuanlong
 * @Date 2025-05-30
 */

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 完成tcp通信  1发1收
 */
public class Tsercer {
    public static void main(String[] args) throws Exception {
        //创建ServerSocket对象，同时为服务端注册端口
        ServerSocket serverSocket = new ServerSocket(8888);
        //2、使用serverSocket对象，调用一个accept方法，等待客户端连接
        Socket socket = serverSocket.accept();
        //3、从socket通信管道中获得一个字节输入流
        InputStream is = socket.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        String rs = dis.readUTF();
        System.out.println(rs);
        //获取客户端地址
        System.out.println(socket.getRemoteSocketAddress());
        dis.close();
        socket.close();
    }
}
